package io.gitee.kotle.player.ui.view.exo

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import androidx.media3.common.MediaItem
import com.google.android.material.progressindicator.CircularProgressIndicator
import io.gitee.kotle.player.core.IPlayer
import io.gitee.kotle.player.core.impl.exoplayer.IExoPlayerMode
import io.gitee.kotle.player.core.isBuffering
import io.gitee.kotle.player.ui.setInvisible

/**
 * 视频加载中view
 */
class ExoPlayerLoadingView : CircularProgressIndicator, IExoPlayerView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        isIndeterminate = true
        trackThickness = dp(2f)
        trackCornerRadius = dp(2f)
        indicatorSize = dp(24f)
    }

    private fun dp(v: Float): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, v, resources.displayMetrics)
            .toInt()
    }

    override fun needPlayerListener(): Boolean {
        return true
    }

    override fun onModelState(
        player: IPlayer<MediaItem, IExoPlayerMode>,
        state: IPlayer.ModelState
    ) {
        super.onModelState(player, state)
        setInvisible(player.isBuffering)
    }
}